package com.cy.ppj.service.dda.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.order.OrderGoodsExtraInfoMapper;
import com.cy.ppj.dao.mapper.order.OrderImageMapper;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.dto.dda.DdaGoodsDiligencingOrderInfoDTO;
import com.cy.ppj.model.enums.order.OrderImageType;
import com.cy.ppj.model.vo.order.ImageListVO;
import com.cy.ppj.model.vo.order.JydOrderDetailAdminVO;
import com.cy.ppj.model.vo.order.OrderImageListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.dda.DdaService;
import com.cy.ppj.service.order.JydOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 与贷贷安系统进行数据同步的接口实现类
 * @Author tanyilin
 * @Date 2019/8/22 15:05
 */
@Slf4j
@Service
public class DdaServiceImpl extends BaseService implements DdaService {

    @Autowired
    private OrderGoodsExtraInfoMapper orderGoodsExtraInfoMapper;
    @Autowired
    private OrderImageMapper          orderImageMapper;
    @Autowired
    private FileService               fileService;
    @Autowired
    private JydOrderService jydOrderService;

    @Override
    public Result<DdaGoodsDiligencingOrderInfoDTO> getDdaGoodsDiligencingOrderInfoDTO(String loanId, Long orderId) {

        DdaGoodsDiligencingOrderInfoDTO infoDTO = orderGoodsExtraInfoMapper.selectDdaGoodsDiligencingOrderInfoDTO(loanId, orderId);
        if(infoDTO == null) {
            return Result.fail("数据异常");
        }
        infoDTO.setSexText(infoDTO.getSex() == 1 ? "男" : "女");
        infoDTO.setBirthday(CommonUtils.getBirthday(infoDTO.getIdNo()));
        infoDTO.setHouseTypeText(infoDTO.getHouseType().getText());

        //查询家具购买单据图片
        List<ImageListVO> imageList = new ArrayList<>();
        List<OrderImageListVO> orderImageList = orderImageMapper.selectOrderImageListByOrderId(orderId, OrderImageType.GOODS_PURCHASE_CONTRACT);
        for (OrderImageListVO bean : orderImageList) {
            ImageListVO imageListVO = new ImageListVO();
            imageListVO.setName(bean.getName());
            imageListVO.setSrc(fileService.getFileUrl(bean.getPath(), Constants.MALL_BUCKET_NAME));
            imageList.add(imageListVO);
        }
        infoDTO.setImageList(imageList);
        infoDTO.setSex(null);
        infoDTO.setIdNo(null);
        infoDTO.setHouseType(null);

        return Result.success("查询数据成功", infoDTO);
    }

    @Override
    public Result getDdaGoodsDiligencingApplyInfo(Long orderId, Long customerId) {

        JydOrderDetailAdminVO detailAdminVO = jydOrderService.getOrderDetail(orderId);

        List<ImageListVO> imageList = jydOrderService.getOrderImageList(orderId, customerId);

        Map<String, Object> map = new HashMap<>();
        map.put("applyInfo", detailAdminVO);
        map.put("imageList", imageList);

        return Result.success("获取申请资料成功", map);
    }


}
